import store from '@/store'
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path: '/free',
    name: 'free',
    component: () => import('../views/Free.vue')
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import('../views/Goods.vue')
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/Order.vue')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/User.vue'),
    redirect:'/user/cart',
    children:[{
      path:'cart',
      component: () => import('../components/user/Cart.vue'),
    }],
    // beforeEnter: (to, from, next) => {
    //   console.log("访问了......");
    //   let token = localStorage.getItem('x-auth-token');
    //   if (token) {
    //     next();
    //   } else {
    //     store.dispatch('showToast/asyncIsShowToast', {
    //       isshow: true,
    //       msg: '你还没有登录',
    //       type: 'danger'
    //     })
    //   }
    // }
  },
  {
    path: '/details',
    name: 'details',
    component: () => import('../views/Details.vue')
  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// router.beforeEach((to, from, next) => {
//   let token = localStorage.getItem("x-auth-token");
//   if (to.path == '/user') {
//     if (token) {
//       next();
//     } else {
//       store.dispatch('showToast/asyncIsShowToast'), {
//         isshow: true,
//         msg: "你还没有登录",
//         type: 'danger'
//       }
//     }
//   } else {
//     next();
//   }
// })

//解决路由重复点击的bug
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
